LevelDB : LevelDB Key / Value database

更新时间:
2024-05-14
下载文档

LevelDB : LevelDB Key / Value database

This module is the encapsulation of the LevelDB database. LevelDB is a Key / Value database. If you are not accessing the database created with LevelDB before, If the amount of data is small JSRE recommends using the LightKV module. User can use the following code to import the leveldb module.

var leveldb = require('leveldb');

Support

The following shows leveldb module APIs available for each permissions.

 User ModePrivilege Mode
leveldb.defaultOpt
leveldb.open
leveldb.repair
leveldb.destroy
leveldb.version
db.close
db.put
db.get
db.delete
db.begin
db.commit
db.rollback
db.iterSeek
db.iterGet

Leveldb Object

leveldb.defaultOpt(flags)

  • flags {String} Flags string.
  • Returns: {Object} LevelDB opens or repairs options.

flags is a string, can be:

flagsDescription
r+Opens database for reading and writing. Fail if the database does not exist.
c+Creates or opens database for reading and writing. Create if it not exists.
cx+Creates database for reading and writing. Fail if it exists.

The returned object contains the following members:

  • flags {String} Flags string.
  • compression {Boolean} Whether to compress. default: false.
  • wSync {Boolean} Whether to write to disk synchronously. default: true.
  • verify {Boolean} Whether to verify. default: false.
  • cacheSize {Integer} Database cache size. default: 1000.
  • wBufSize {Integer} Database write buffer size. default: 16 * 1024.
  • maxFileSize {Integer} Maximum size of database file. default: 1024 * 1024 * 1024.

Get a default database option, no special circumstances do not need to modify the default value.

Example

var opt = leveldb.defaultOpt('c+');

leveldb.open(dbPath[, opt])

  • dbPath {String} Database path.
  • opt {Object} LevelDB option object. default: leveldb.defaultOpt('c+').
  • Returns: {Object} A LevelDB object.

Open or create a database with the specified options.

Example

var db = leveldb.open('./user.ldb');

leveldb.repair(dbPath[, opt])

  • dbPath {String} Database path.
  • opt {Object} LevelDB option object. default: leveldb.defaultOpt('c+').
  • Returns: {Boolean} Whether the database is repaired.

When the database cannot be opened due to an unexpected situation, you can use this function to repair and then try to open again.

Example

leveldb.repair('./user.ldb');

leveldb.destroy(dbPath)

  • dbPath {String} Database path.
  • Returns: {Boolean} Whether the database is destroyed.

Delete a database, if the database can not be repaired, you can delete the database after backup.

Since LevelDB is a directory, you can use the zip module for compressed and backups.

Db Object

db.close()

Close the database.

db.put(key, value)

  • key {String} Keyword.
  • value {String} Value string.
  • Returns: {Boolean} Whether the put is successful.

Insert or rewrite a record.

Example

db.put('Jack', '123456');
db.put('Rose', '789012');

db.put(key, buffer[, offset[, length]])

  • key {String} Keyword.
  • buffer {Buffer} Record data buffer.
  • offset {Integer} Buffer offset. default:0.
  • length {Integer} Record length. default:buffer.length.
  • Returns: {Boolean} Whether the put is successful.

Insert or rewrite a binary record.

Example

var buf = new Buffer([1, 2, 3]);
db.put('Carl', buf);

db.get(key)

  • key {String} Keyword.
  • Returns: {Buffer} Key content.

Find the contents of a key and return a buffer object to store the content.

Example

var res = db.get('Jack');
if (res) {
  console.log('Jack', res.toString());
}

db.delete(key)

  • key {String} Keyword.
  • Returns: {Boolean} Whether the delete is successful.

Delete a specified record by key.

Example

db.delete('Carl');

db.begin()

Similar to the SQL begin; statement, turning on a batch write. After this db.put() operation will be written to a cache.

db.commit()

Similar to the SQL commit; statement, do batch write commit work.

Example

db.begin();
db.put('Jack1', '1234561');
db.put('Jack2', '1234562');
db.put('Jack3', '1234563');
db.put('Jack4', '1234564');
db.commit();

Only the committed data will be confirmed by the database.

db.rollback()

Discard the uncommitted batch write and rollback to the last confirmed state.

Example

db.begin();
db.put('Jack1', '1234561');
db.put('Jack2', '1234562');
db.put('Jack3', '1234563');
db.put('Jack4', '1234564');
db.rollback(); // Discard!

Database Traversal

db.iterSeek(position)

  • position {Integer} Position of cursor.
  • Returns: {Boolean} Whether the seek is successful.

position can be:

  • leveldb.SEEK_FIRST The first record.
  • leveldb.SEEK_LAST The last record.
  • leveldb.SEEK_NEXT The next record.
  • leveldb.SEEK_PREV The previous record.

Set the current cursor position.

db.iterGet()

  • Returns: {Object} Record at the current cursor.

The returned object contains the following members:

  • key {String} Keyword.
  • value {Buffer} Record data buffer.

Get the record at the current cursor.

Example

var record = null;

db.iterSeek(leveldb.SEEK_FIRST);
record = db.iterGet();
if (record) {
  console.log(record.key, record.value.toString());
}

db.iterSeek(leveldb.SEEK_NEXT);
record = db.iterGet();
if (record) {
  console.log(record.key, record.value.toString());
}
文档内容是否对您有所帮助?
有帮助
没帮助